package com.example.pceb.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pceb.entity.IpTransaction;
import com.example.pceb.mapper.IpTransactionMapper;
import com.example.pceb.service.IpTransactionService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IpTransactionServiceImpl extends ServiceImpl<IpTransactionMapper, IpTransaction>
        implements IpTransactionService {

    @Override
    public IPage<IpTransaction> findPage(int page, int size, String status) {
        LambdaQueryWrapper<IpTransaction> query = new LambdaQueryWrapper<>();
        if (status != null) query.eq(IpTransaction::getStatus, status);
        query.orderByDesc(IpTransaction::getCreateTime);
        return this.page(new Page<>(page, size), query);
    }

    @Override
    public List<IpTransaction> findBySeller(String username) {
        return this.list(new LambdaQueryWrapper<IpTransaction>()
                .eq(IpTransaction::getSellerUsername, username)
                .orderByDesc(IpTransaction::getCreateTime));
    }

    @Override
    public boolean deleteByIdAndUsername(Long id, String username) {
        LambdaQueryWrapper<IpTransaction> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IpTransaction::getId, id)
                .eq(IpTransaction::getSellerUsername, username)
                .eq(IpTransaction::getStatus, "在售"); // 只能删除在售的

        return this.remove(wrapper);
    }
}
